package com.ita.iosa.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ita.iosa.dao.MerchantDao;
import com.ita.iosa.po.Merchant;
import com.ita.iosa.util.DbUtil;

public class MerchantDaoImpl implements MerchantDao{

	@Override
	public Merchant selectOneMerchant(int mid) {
		Merchant merchant = new Merchant();
		String sql = "select * from merchant where mid=?";
		
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();

		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, mid);
			rs = pst.executeQuery();
			while (rs.next()) {
				merchant.setMid(rs.getInt("mid"));
				merchant.setMname(rs.getString("mname"));
				merchant.setType(rs.getString("type"));
				merchant.setAddress(rs.getString("address"));
				merchant.setTel(rs.getString("tel"));
				merchant.setLogo(rs.getString("logo"));
				merchant.setInfo(rs.getString("info"));
				merchant.setEmail(rs.getString("email"));
				merchant.setPassword(rs.getString("password"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, rs);
		}
		
		
		return merchant;
	}

	@Override
	public int addMerchant(Merchant merchant) {
		String sql = "insert into merchant(mid,mname,type,address,tel,logo,info,email,password) values (my_seq.nextval,?,?,?,?,?,?,?,?)";

		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();

		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setString(1, merchant.getMname());
			pst.setString(2, merchant.getType());
			pst.setString(3, merchant.getAddress());
			pst.setString(4, merchant.getTel());
			pst.setString(5, merchant.getLogo());
			pst.setString(6, merchant.getInfo());
			pst.setString(7, merchant.getEmail());
			pst.setString(8, merchant.getPassword());
			
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	@Override
	public int selectMerchantMid(String tel) {
		String sql = "select mid from merchant where tel=?";
		int mid = -1;
		
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();

		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, tel);
			rs = pst.executeQuery();
			while (rs.next()) {
				mid = rs.getInt("mid");
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, rs);
		}
		return mid;
	}

	@Override
	public int updateMerchant(Merchant merchant) {
		String sql = "update merchant set tel=?,mname=?,type=?,logo=?,info=?,address=?,password=?,email=? where mid=?";

		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();

		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setString(1, merchant.getTel());
			pst.setString(2, merchant.getMname());
			pst.setString(3, merchant.getType());
			pst.setString(4, merchant.getLogo());
			pst.setString(5, merchant.getInfo()); 
			pst.setString(6, merchant.getAddress());
			pst.setString(7, merchant.getPassword());
			pst.setString(8, merchant.getEmail());
			pst.setInt(9, merchant.getMid());
	
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

}
